﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using YDWin.Base;
using YDCommon;
using YDServices;
using DevExpress.Utils;

namespace YDWin.Printing
{
    public partial class PicForm :  AddForm
    {
        PrintingService svr = new PrintingService();
        static string PicAddress = "";
        public long orderdetail_id = 0;

        public PicForm()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 选择图片
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSelect_Click(object sender, EventArgs e)
        {
            ShowPic(pic);
        }

        /// <summary>
        /// 选择图片
        /// </summary>
        /// <param name="picEdit"></param>
        public static void ShowPic(PictureEdit picEdit)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            //ofd.InitialDirectory = @"D:\";
            ofd.Filter = "Image Files(*.JPG;*.PNG;*.jpeg;*.GIF;*.BMP)|*.JPG;*.PNG;*.GIF;*.BMP;*.jpeg|All files(*.*)|*.*";
            ofd.RestoreDirectory = true;
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                PicAddress = ofd.FileName;
                Image imge = Image.FromFile(PicAddress);
                Bitmap bm = new Bitmap(imge, picEdit.Width, picEdit.Height);
                picEdit.Image = bm;
            }
        }

        /// <summary>
        /// 上传到数据库
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnUpload_Click(object sender, EventArgs e)
        {
            if (PicAddress != null)
            { 
                byte[] pic = Utils.GetContent(PicAddress); 

                WaitDialogForm sdf = new WaitDialogForm("", "正在保存......");
                svr.UploadPic(orderdetail_id, pic); 
                sdf.Close();

                if (svr.HasError)
                {
                    Utils.ShowPrompt(MessageType.mtError, "图片上传失败：" + svr.Errors);
                    svr.Errors.Clear();
                    return;
                }

                this.DialogResult = DialogResult.OK;
                this.Close();

            }
            else
            { 
                Utils.ShowPrompt(MessageType.mtWarning, "请先选择图片!" );
            }
        }
    }
}